#ifndef ITEM_H
#	define ITEM_H

#include <iostream>
#include <boost/pool/pool.hpp>
#include <boost/pool/object_pool.hpp>

// BDB 
#include <db_cxx.h>

#include "../../include/config.h"

#include "../Common/mem_pool.h"
#include "HappyEngineConfig.h"
#include "item_data.h"


using namespace std;
using namespace HappyDB::Common;


namespace HappyDB
{
namespace StorageEngine
{



template <typename T>
class CItem 
{
public:
	CItem (T * pData, u_int32_t ulen)
		:m_pItemPool(ulen)
	{
		this->m_pData = pData;
		this->m_ulen = ulen;

		ItemInit();
	}

	~CItem()
	{
	}

public:
	HappyDB::Common::CMemPool<ITEM>		m_pItemPool;
	T *										m_pData;
	u_int32_t								m_ulen;

public:
	// item alloc and free
	void ItemInit()
	{
	}

	// normal alloc 
	ITEM * ItemAlloc(void)
	{
		ITEM * s;

		void * p = this->m_pItemPool.malloc();
		memset(p, 0, m_ulen);

		s = (ITEM*) p;

		return s;
	}

	
	ITEM * ItemAlloc(u_int32_t ulen)
	{
	}

	// call it when one connect is starting
	ITEM * ItemAlloc(char * key, const size_t nkey, const int flags, const int nbytes)
	{
		item * s;

		s = this->m_pItemPool.malloc();

		return s;
	}

	void ItemFree(ITEM * it)
	{
		if (it)
		{
			this->m_pItemPool.free(it);
		}
	}



	// item function 
	ITEM * ItemGet(char * key, size_t nkey)
	{
		ITEM *it = NULL;
		bool stop;
		int ret;

		// alloc 
		it = this->ItemAlloc();
		if (it == 0)
		{
			return NULL;
		}

		stop = false;
		while (!stop)
		{
			ret = this->m_pData->ItemGet(key, nkey, it, m_ulen);
			if (ret == -1)
			{
				// can't find key 
				stop = true;
				this->ItemFree(it);
				it = NULL;
				break;
			}
			else if (ret == 0)
			{
				// Success 
				stop = true;
				break;
			}
			else
			{
				
			}
		}


		return it;
			
	}

	int ItemPut(char * key, size_t nkey, ITEM * it)
	{
		return 0;
	}

};



}
}


#endif